<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link rel="stylesheet" href="../tools/bootstrap-3.3.7-dist/css/bootstrap.min.css">
  <link rel="stylesheet" href="../css/base.css">
  <link rel="stylesheet" href="../css/login.css">
  <style>
      .inputWidth{
        width: 200px;
        display: inline-block;
      }

      .spanspan{
        color:red;
        font-size: 20px;
      }
      .cover{
        width: 100%;
        height: 100%;
        background: rgba(0,0,0,.5);
      }
      #sc{
        margin: 0 0 0 20px;
        font-size: 20px;
      }
  </style>
</head>
<body>

    <div class="jumbotron">
      <h1>欢迎注册 bk2102 SHOP</h1>
      <div class="login">
        <div class="form-horizontal">
          <div class="form-group">
            <label for="username" class="col-sm-2 control-label">用户名</label>
            <div class="col-sm-10">
              <input name="userName" type="text" class="form-control inputWidth"  placeholder="用户名">
              <span name="nameSpan" class="spanspan"></span>
            </div>
          </div>
          <div class="form-group">
            <label for="password" class="col-sm-2 control-label">密码</label>
            <div class="col-sm-10">
              <input name="userPwd1" type="password" class="form-control inputWidth"  placeholder="密码">
              <span class="spanspan"></span>
            </div>
          </div>

          <div class="form-group">
            <label for="password" class="col-sm-2 control-label">确认密码</label>
            <div class="col-sm-10">
              <input name="userPwd2" type="password" class="form-control inputWidth"  placeholder="确认密码">
              <span class="spanspan"></span>
              
            </div>
          </div>

          <div class="form-group">
            <label for="password" class="col-sm-2 control-label">验证码</label>
            <div class="col-sm-10">
              <input name="vc" type="text" class="form-control inputWidth" id="ac" placeholder="验证码"> <a id="sc"></a><br>
              <span class="spanspan"></span>
            </div>
          </div>

          <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
              <button class="btn btn-default">注册</button>
            </div>
          </div>
        </div>
      </div>
    </div>
    <script src="../tools/jquery.min.js"></script>
    <script src="../js/tools.js"></script>
    <script src="../js/ajax.js"></script>
    <script>
      $('#sc').html(setVc()).click(function(){$(this).html(setVc())});
      //失去焦点时 触发程序
      //向后端发送请求 严重用户名是否可用
      $('[name ="userName"]').blur(function(){
        //触发ajax程序 向后端 发送请求 携带账号密码
        //promise写法
        const p = pjQueryAjax('../server/goods_select.php','post',{userName:$(this).val()},'json');
        p.then((res)=>{
          if(res.result === 1){
              //用户名可用
              $('[name="nameSpan"]').html('可用');
            }else{
              $('[name="nameSpan"]').html('bu可用');
            }

        })

      })
      
      //点击button 触发注册功能
      $('button').click(function(){
        //执行一些验证程序 如果验证不通过 终止
        if($('[name = userPwd1]').val()!== $('[name = "userPwd2"]').val()){
          //输入提示信息
          window.alert('两次密码不一');
          $('#sc').html( setVc() );
          //执行return
          return;
        }
        //输入的验证码 和标签的验证码一致
        if($('[name = "vc"]').val().toLowerCase() !==$('#sc').html().toLowerCase()){
          window.alert('验证码错误');
          $('#sc').html( setVc() );
          return;
        }


        //触发ajax 向后端发送请求

        //promise写法
        const p = pjQueryAjax('../server/goods_register.php','post',{userName:$('[name="userName"]').val(), userPwd:$('[name="userPwd1"]').val()},'json');
        p.then((res)=>{
          if(res.result === 1){
              window.alert('注册成功，跳转首页');
              window.location.href='./index.html';
            }else{
              window.alert('注册失败');
              $('#sc').html( setVc() );
            }

        })


      })

    </script>
</body>
</html>
